package com.payment.ktb.activity.main1;

import android.app.Dialog;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;

import com.payment.ktb.Interface.AlertInterface;
import com.payment.ktb.R;
import com.payment.ktb.activity.BaseActivity;
import com.payment.ktb.constants.ConstantsUser;
import com.payment.ktb.utils.AlertDialogUtils;
import com.payment.ktb.utils.SharedPreferencesUtils;
import com.payment.ktb.utils.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by syd on 2016/8/2.
 */
public class BanKaJXActivity extends BaseActivity {

    @BindView(R.id.wv_banka)
    WebView wv_banka;
//    String  bankaUrl = "https://wm.cib.com.cn/application/cardapp/Fast/TwoBar/view?id=52f93bb604a14b4fae5afbbec91b7120";
    String  bankaUrl = "http://ktb.4006007909.com/shop/bank/clb?token=" +  SharedPreferencesUtils.getString(ConstantsUser.userToken);
    String idSelf = SharedPreferencesUtils.getString(SharedPreferencesUtils.getString(ConstantsUser.phone) + ConstantsUser.SHOPCERT);
    boolean canGoNextPage = true;
    int dialogCount = 0 ;
    String currentUrl = "";
    String BaseInfoUrl = "https://wm.cib.com.cn/application/cardapp/Fast/BaseInfo/view";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_banka);
        ButterKnife.bind(this);
        initTitle("办卡");
        dialogCount = 0;
        initWebView();
        wv_banka.addJavascriptInterface(new JavaScriptInterface(),"android");
        wv_banka.loadUrl(bankaUrl);



        wv_banka.setWebViewClient(new HelloWebViewClient ());
        LinearLayout llBack = (LinearLayout) findViewById(R.id.ll_title_back);
        llBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (wv_banka.canGoBack()) {
                    wv_banka.goBack();
                } else {
                    finish();
                }
            }
        });
    }

    private void initWebView() {

        wv_banka.setVerticalScrollBarEnabled(false);
        wv_banka.setHorizontalScrollBarEnabled(false);
        wv_banka.getSettings().setJavaScriptEnabled(true);
        wv_banka.getSettings().setSupportZoom(true);
        wv_banka.getSettings().setDomStorageEnabled(true);
        wv_banka.requestFocus();
        wv_banka.getSettings().setUseWideViewPort(true);
        wv_banka.getSettings().setLoadWithOverviewMode(true);
        wv_banka.getSettings().setSupportZoom(true);
        wv_banka.getSettings().setBuiltInZoomControls(true);
        wv_banka.requestFocusFromTouch();
        wv_banka.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
    }


    private class HelloWebViewClient extends WebViewClient {


        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
            if (BaseInfoUrl.equals(currentUrl)) {
                wv_banka.loadUrl("javascript:window.android.saveId(document.getElementById('indentificationId').value)");
            }

        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            currentUrl = url;
            if (canGoNextPage) {
                view.loadUrl(url);
            }
            return true;
        }

    }


    /**
     * 判断输入的身份证是否是本人的登录用户的身份证
     */
    public class  JavaScriptInterface {

        @JavascriptInterface
        public void saveId(String iden){

            iden = iden.trim();
            if (!TextUtils.isEmpty(iden)) {
                if (!iden.equals(idSelf)) {
                    if (dialogCount == 0) {
                        AlertDialogUtils.showAlertDialog(mContext, "请输入本人身份证号码", new AlertInterface() {
                            @Override
                            public void confirm(Dialog alertDialog) {
                                dialogCount = 0;
                                alertDialog.dismiss();
                            }
                        });
                    }
                    ToastUtils.showToast("请输入本人身份证号码");
                    dialogCount ++ ;
                    canGoNextPage = false;
                } else {
                    canGoNextPage = true;
                }
            }

        }

    }


}
